// pages/order/ordersubmit/index.js
var server = require('../../../utils/server');
var tp;
var pay_points;
var points_rate;
Page({
  data: {
    items: [
      { name: 'shentong', value: '快递送货', checked: 'true'},
      { name: 'smzt', value: '上门自提'},
    ],
   address_status:true,
   use_money: 0,
   hd_id:'',
   shipping_code:'shentong',
   use_point: 0, 
   check: ['true', ''], 
   "coupon": [], 
   cv: '使用優惠券',
    cpos: -1, 
    "couponCode": ''
     },
  addressSelect: function () {
    wx.navigateTo({
      url: '../../address/select/index'
    });
  },


  radioChange: function(e){
    var shipping_code=e.detail.value;
    var address_status=true;
    console.log(shipping_code);
    if(shipping_code=='smzt'){
       address_status =false;
    } 
     this.setData({
      address_status: address_status,
      shipping_code: shipping_code,
    });
  },
  bindChange: function (e) {

    var use_money = e.detail.value;

    this.setData({
      use_money: use_money,
    });
  },

 




  bindChangeOfcoupon: function (e) {
    var couponCode = e.detail.value;

    this.setData({
      couponCode: couponCode,
    });
  },
  bindChangeOfPoint: function (e) {
    var use_point = e.detail.value;
    this.setData({
      use_point: use_point,
    });
  },
  bindPickerChange: function (e) {
    var value = e.detail.value;
    var cv = this.data.coupon[value];
    this.setData({ cv: cv, cpos: value });



    this.useCoupon();


  },
  useCoupon: function () {
    if (this.data.cpos == -1)
      return;


    var that=this;
    var app =getApp();
    var hd_id =this.data.hd_id;
    var shipping_code =this.data.shipping_code;
    var user_id = app.globalData.userInfo.user_id

    var index =this.data.cpos;
    var coupon_id =this.data.couponList[index].id;

    // console.log(index); 
    // console.log(this.data.couponList[index].id);return false;

 server.getJSON('/Cart/cart3/nosubmit/1/user_id/' + user_id + "/shipping_code/" + shipping_code +"/hd_id/" + hd_id+"/coupon_id/"+coupon_id, function (res) {
        console.log(res.data); 
      if(res.data.status!=1){
         wx.showToast({
            title: res.data.msg,
            duration: 1000
          });
          return;
        }else{
            that.setData({
              totalPrice :res.data.result
            });
          } 
      

 });


    // var money = this.data.couponList[this.data.cpos].money;
    // var totalObj = this.data.totalPrice;
    // totalObj.total_fee = totalObj.total_fee - money
    // if (totalObj.total_fee < 0)
    //   totalObj.total_fee = 0;
    // this.setData({ totalPrice: totalObj });
  },
  use: function () {
    //totalPrice:
    var user_money = getApp().globalData.userInfo.user_money;
    var use_money = this.data.use_money;
    user_money = parseFloat(user_money)
    use_money = parseFloat(use_money)
    if (user_money < use_money) {
      var totalObj = this.data.totalPrice;

      var use_point = this.data.use_point;
      var use_point = parseInt(use_point)
      use_point = use_point - use_point % parseInt(points_rate);
      var m = tp - use_point / parseInt(points_rate)
      totalObj.total_fee = m
      this.setData({ totalPrice: totalObj });

      this.useCoupon();

      this.setData({ use_money: 0 });
      wx.showToast({
        title: '请输入小余当前余额',
        duration: 1000
      });
      return;
    }
    var use_point = this.data.use_point;
    var use_point = parseInt(use_point)
    use_point = use_point - use_point % parseInt(points_rate);
    var m = tp - use_point / parseInt(points_rate)

    var totalPrice = m - use_money;
    if (totalPrice < 0)
      totalPrice = 0;
    var totalObj = this.data.totalPrice;
    totalObj.total_fee = totalPrice
    this.setData({ totalPrice: totalObj });

    this.useCoupon();
  },
  use_point: function () {
    //totalPrice:
    var user_point = pay_points;
    var use_point = this.data.use_point;
    use_point = parseInt(use_point)
    use_point = use_point - use_point % parseInt(points_rate);
    if (parseInt(user_point) < use_point) {
      var totalObj = this.data.totalPrice;
      var m = tp - this.data.use_money
      totalObj.total_fee = m
      this.setData({ totalPrice: totalObj });

      this.setData({ use_point: 0 });
      this.useCoupon();
      wx.showToast({
        title: '请输入小余当前积分',
        duration: 1000
      });
      return;
    }
    var m = tp - this.data.use_money;
    var totalPrice = m - (use_point / parseInt(points_rate));
    if (totalPrice < 0)
      totalPrice = 0;
    var totalObj = this.data.totalPrice;
    totalObj.total_fee = totalPrice
    this.setData({ totalPrice: totalObj });
    this.useCoupon();
  },

    


  onShow: function () {
    var app = getApp();
     var user_id = app.globalData.userInfo.user_id

    server.getJSON('/Cart/get_cart_info/user_id/' + user_id, function (res) {
      if (res.data.status != 1) {
        wx.switchTab({
              url: "../../cart/cart"
            });
            return;
      }
    });

   if(!app.globalData.login)
        {
             wx.switchTab({
              url: '/pages/member/index/index'
            });
        }

    var cartIds = app.globalData.cartIds;
    var amount = app.globalData.amount;
    var hd_id = app.globalData.hd_id;

    this.setData({ cartIds: cartIds, amount: amount,hd_id:hd_id });
    this.getCarts(cartIds,hd_id);
    // 页面初始化 options为页面跳转所带来的参数
  },
  initData: function () {
    var app = getApp();
    pay_points = app.globalData.userInfo.pay_points;
    var user_money = app.globalData.userInfo.user_money;
    this.setData({ freemoney: user_money, pay_points: pay_points });
  },
  formSubmit: function (e) {

    // user 
     
    var shipping_code = this.data.shipping_code;
     var formId = e.detail.formId;
    // var formId = 1519808146350;

    if(this.data.address!=null){
      var address_id = this.data.address.address_id
    }else{
      if(shipping_code!='smzt'){
        wx.showToast({
          title: '请填写收货地址！',
          icon: 'error',
          duration: 1000
        });
        wx.navigateTo({
          url: '/pages/address/select/index'
          // url: '../../../../order/checkout/checkout?cartIds=' + cartIds +'&hd_id=' +hd_id + '&amount=' + this.data.total
        });
        return false;
      }else{
        var address_id =0;
      }
    }
   
    var user_id = getApp().globalData.userInfo.user_id





    var use_money = this.data.use_money
    var hd_id =this.data.hd_id
    var pay_points = this.data.use_point
    var that = this;
    var app = getApp();
    var couponTypeSelect = this.data.check[0] == "true" ? 1 : 2;
    var coupon_id = 0;
    if (this.data.cpos != -1) {
      coupon_id = this.data.couponList[this.data.cpos].id;
    }
    var couponCode = this.data.couponCode;

    server.getJSON('/Cart/cart3/act/submit_order/formId/'+formId+'/user_id/' + user_id + "/hd_id/" + hd_id +"/address_id/" + address_id +"/shipping_code/" + shipping_code + "/user_money/" + use_money + "/pay_points/" + pay_points + "/couponTypeSelect/" + couponTypeSelect + "/coupon_id/" + coupon_id + "/couponCode/" + couponCode, function (res) {
        

      if (res.data.status != 1) {
        wx.showToast({
          title: res.data.msg,
          duration: 2000
        });
        return;
      }

      var result = res.data.result

      app.globalData.wxdata = res.data.data
      app.globalData.order = res.data.order
      if (res.data.status == 1) {
        wx.showToast({
          title: '提交成功',
          duration: 2000
        });



        setTimeout(function () {

          if (res.data.order.pay_status == 1) {
            wx.switchTab({
              url: "../../member/index/index"
            });
            return;
          }

          wx.navigateTo({
            url: '../payment/payment?order_id=' + result
          });
        }, 2000);

      }


    });
  },

  getCarts: function (cartIds,hd_id) {
    var user_id = getApp().globalData.userInfo.user_id
    var that = this
    var app = getApp()

    server.getJSON('/Cart/cart2/user_id/' + user_id+'/hd_id/'+hd_id, function (res) {
      var user_data = app.globalData.userInfo;
      user_data.user_money = res.data.result.userInfo.user_money;
      user_data.pay_points = res.data.result.userInfo.pay_points;
      app.globalData.userInfo = user_data
      var address = res.data.result.addressList


      var cartList = res.data.result.cartList
      var userInfo = res.data.result.userInfo
      var totalPrice = res.data.result.totalPrice
      tp = totalPrice.total_fee
      points_rate = res.data.result.points
      that.setData({ 
        address: address, 
        cartList: cartList,
         userInfo: userInfo,
          totalPrice: totalPrice
           });

      var couponList = res.data.result.couponList
      var ms = that.data.coupon
      for (var i in couponList) {
        ms.push(couponList[i].name);
      }
      that.setData({ 
        coupon: ms, 
        couponList: couponList
         });
      that.initData();
    })
  },
  check1: function () {
    this.setData({ check: ['true', ''] });
  },
  check2: function () {
    this.setData({ check: ['', 'true'] });
  },
  onReady: function () {
    // 页面渲染完成
  },

  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})